

import Common from './common'; 
import {getQueryCondition} from 'src/hrpub/common/utils/utils'

export default class ExportAction extends Common {
    constructor(comp) {
        super();
        this.comp = comp;
    }

    // 订阅打开弹窗事件
    subscribe = () => {
        this.pubSub.subscribe('showExportModal', () => {
            this.showModal();
        });
    }

    didMount = () => {
        this.subscribe();
    }

    willUnMount = () => {
        this.pubSub.unSubscribe('showExportModal');
    }

    // 关闭导出弹窗
    closeModal = () => {
        this.update({
            exportModal: false
        });
    }

    // 打开到处弹窗
    showModal = () => {
        this.update({
            exportModal: true
        });
    }

    // 修改花名册参照
    changeRefer = (value) => {
        this.update({
            exportValue: value
        });
    }

    // 确认导出
    doExport = async () => {
        const {
            props: {
                search,
                sca: {
                    page,
                    pageInfo,
                    orgValue,
                    exportValue,
                    allPks
                },
                editTable,
                meta,
                dispatch
            }
        } = this.comp;

        let template = meta.getMeta();
        let searchId = this.searchMap[page];
        let queryParam = getQueryCondition(this.comp.props,searchId)
        let tableId = this.listMap[page];
        let tableData = editTable.getAllData(tableId);
        let masterKeyMap = {
            entry: 'pk_entryapply',
            leave: 'pk_hi_stapply',
            regular: 'pk_hi_regapply'
        };

        // let allpks = tableData.rows.map((item) => {
        //     return item.values[masterKeyMap[page]].value;
        // });

        let postData = {
            ...queryParam,
            psnjob_pks: allPks.join(','),
            pageInfo: pageInfo,
            pk_org: orgValue[page].refpk,
            template_id: exportValue ? exportValue.refpk : '',
            queryType: this.listMap[page]
        }

        this.doExportPost({
            data: JSON.stringify(postData)
        });
        this.update({
            exportModal: false
        });
    }

    // 发送导出请求
    doExportPost = (postData) => {
        const {
            props: {
                dispatch
            }
        } = this.comp;
        
        try {
            this.modelDoExport({
                postData: postData
            });
        }
        catch(e) {
            console.error(`doExportPost: ${e}`);
        }
        return false;
    }
}